<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Unit tests for tinymce.ForceBlocks</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-git.css" type="text/css" />
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
<script src="qunit/connector.js"></script>
<script type="text/javascript" src="qunit/runner.js"></script>
<script type="text/javascript" src="js/utils.js"></script>
<script type="text/javascript" src="js/tiny_mce_loader.js"></script>
<script>
var editor;

QUnit.config.reorder = false;
QUnit.config.autostart = false;

module("tinymce.ForceBlocks", {
	autostart: false,
	setup: function() {
		editor.settings.forced_root_block = 'p';
	}
});

function pressArrowKey(evt) {
	var dom = editor.dom, target = editor.selection.getNode();

	evt = tinymce.extend({keyCode: 37}, evt);

	dom.fire(target, 'keydown', evt);
	dom.fire(target, 'keypress', evt);
	dom.fire(target, 'keyup', evt);
}

test('Wrap single root text node in P', function() {
	editor.getBody().innerHTML = 'abcd';
	setSelection('body', 2);
	pressArrowKey();
	equal(cleanHtml(editor.getBody().innerHTML), '<p>abcd</p>');
	equal(editor.selection.getNode().nodeName, 'P');
});

test('Wrap single root text node in P but not table sibling', function() {
	editor.getBody().innerHTML = 'abcd<table><tr><td>x</td></tr></table>';
	setSelection('body', 2);
	pressArrowKey();
	equal(cleanHtml(editor.getBody().innerHTML), '<p>abcd</p><table><tbody><tr><td>x</td></tr></tbody></table>');
	equal(editor.selection.getNode().nodeName, 'P');
});

test('Wrap root em in P but not table sibling', function() {
	editor.getBody().innerHTML = '<em>abcd</em><table><tr><td>x</td></tr></table>';
	setSelection('em', 2);
	pressArrowKey();
	equal(cleanHtml(editor.getBody().innerHTML), '<p><em>abcd</em></p><table><tbody><tr><td>x</td></tr></tbody></table>');
	equal(editor.selection.getNode().nodeName, 'EM');
});

test('Wrap single root text node in DIV', function() {
	editor.settings.forced_root_block = 'div';
	editor.getBody().innerHTML = 'abcd';
	setSelection('body', 2);
	pressArrowKey();
	equal(cleanHtml(editor.getBody().innerHTML), '<div>abcd</div>');
	equal(editor.selection.getNode().nodeName, 'DIV');
});

test('Remove empty root text nodes', function() {
	var body = editor.getBody();

	editor.settings.forced_root_block = 'div';
	editor.getBody().innerHTML = 'abcd<div>abcd</div>';
	setSelection('body', 2);
	body.insertBefore(editor.getDoc().createTextNode(''), body.firstChild);
	body.appendChild(editor.getDoc().createTextNode(''));
	pressArrowKey();
	equal(cleanHtml(body.innerHTML), '<div>abcd</div><div>abcd</div>');
	equal(editor.selection.getNode().nodeName, 'DIV');
	equal(body.childNodes.length, 2);
});

tinyMCE.init({
	mode : "exact",
	elements : "elm1",
	theme : "advanced",
	add_unload_trigger : false,
	apply_source_formatting : false,
	schema: 'html5',
	entities : 'raw',
	valid_styles : {
		'*' : 'color,font-size,font-family,background-color,font-weight,font-style,text-decoration,float,margin,margin-top,margin-right,margin-bottom,margin-left,display'
	},
	init_instance_callback : function(ed) {
		editor = ed;

		ed.onNodeChange.addToTop(function() {
			return false;
		});

		QUnit.start();
	}
});
</script>
</head>
<body>
	<h1 id="qunit-header">Unit tests for tinymce.ForceBlocks</h1>
	<h2 id="qunit-banner"></h2>
	<div id="qunit-testrunner-toolbar"></div>
	<h2 id="qunit-userAgent"></h2>
	<ol id="qunit-tests"></ol>
	<textarea id="elm1" name="elm1"></textarea>
</body>
</html>
